package com.example.springbootlucenedemo.common.lucene.bean;

import cn.hutool.json.JSONObject;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.util.ClassUtils;

import java.io.Serializable;

@Data
public  class Message implements Serializable {
    Object object;
    Class aclass;
    public Message(Object object) {
        this.object = object;
        if (object.getClass().toString().contains("$") ){
            this.aclass = ClassUtils.getUserClass(object).getSuperclass();
        }else{
            this.aclass =object.getClass();
        }
    }
    @SneakyThrows
    public Message(String message){
        JSONObject jsonObject = new JSONObject(message);
        String aClass = jsonObject.getStr("aclass");
        this.aclass = Class.forName(aClass);
        this.object = jsonObject.get("object",this.aclass);
    }
}